local db = TUNING.MOD_MOD_PONO.badge.mod_pono_energy

---@class components
---@field mod_pono_energy component_mod_pono_energy

local function on_val(self, value)
    self.inst.replica.mod_pono_energy:SetVal(value)
    -- 触发能量值变化事件，用于更新工作效率
    if self.inst and self.inst.PushEvent then
        self.inst:PushEvent("mod_pono_energy_val_change", {val = value})
    end
end

---@class component_mod_pono_energy
---@field inst ent
---@field val number
local mod_pono_energy = Class(

---@param self component_mod_pono_energy
---@param inst ent
function(self, inst)
    self.inst = inst
    self.val = 0
end,
nil,
{
    val = on_val,
})

function mod_pono_energy:OnSave()
    return {
        val = self.val
    }
end

function mod_pono_energy:OnLoad(data)
    -- Use SetVal to ensure clamping and trigger any observers
    if data and data.val ~= nil then
        self:SetVal(data.val)
    else
        -- preserve existing value if no data provided
        self.val = self.val or 0
    end
    -- Notify listeners that the component finished loading (hook can listen and initialize safely)
    if self.inst and self.inst.PushEvent then
        self.inst:PushEvent("mod_pono_energy_onload", {val = self.val})
    end
end

function mod_pono_energy:SetVal(val)
    self.val = math.clamp(val, 0, db.max)
end

function mod_pono_energy:GetMax()
    return db.max
end

function mod_pono_energy:DoDelta(num)
    self.val = math.clamp(self.val + num, 0, db.max)
end

function mod_pono_energy:IsMax()
    return self.val >= db.max
end

function mod_pono_energy:GetVal()
    return self.val
end

return mod_pono_energy